package com.google.code.rgbgoesbetween.gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.google.code.rgbgoesbetween.GlobalConstants;
import com.google.code.rgbgoesbetween.R;
import com.google.code.rgbgoesbetween.data.Item;
import com.google.code.rgbgoesbetween.data.SearchEngineCriteria;
import com.google.code.rgbgoesbetween.data.Item.ItemStatus;
import com.google.code.rgbgoesbetween.services.DataProvider;
import com.google.code.rgbgoesbetween.services.SessionManager;

public class ItemListDisplayActivity extends Activity implements
		OnItemClickListener {

	private boolean isBlank = false;
	private ItemAdapter itemAdapter;
	private EditText edit;
	private Spinner spinner;
	private ListView listView;
	private Item[] items;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_list_display);

		spinner = (Spinner) this.findViewById(R.id.item_status_spinner);
		edit = (EditText) this.findViewById(R.id.search_edit_text);
		listView = (ListView) this.findViewById(R.id.item_display_list_view);

		if (this.getIntent().getBooleanExtra(GlobalConstants.FROM_HOME, false)) {
			List<Integer> ids = SessionManager.getSessionManager().getProfile()
					.getItemIDList();
			Log.d("ITEM_LIST_DISPLAY_ACTIVITY", Arrays.toString(ids.toArray()));
			if (ids.size() == 0) {
				isBlank = true;
				Item i = new Item();
				i.setName("You have no items reported!");
				items = new Item[] { i };
			} else {
				isBlank = false;
				items = DataProvider.getDataProvider().requestItemByIDs(ids);
			}
			itemAdapter = new ItemAdapter(this, items);
			listView.setAdapter(itemAdapter);
		} else {
			int[] ids = getIntent().getIntArrayExtra(GlobalConstants.ITEM_ID);
			Log.d("ITEM_LIST_DISPLAY_ACTIVITY", Arrays.toString(ids));
			Item[] items;
			if (ids.length == 0) {
				isBlank = true;
				Item i = new Item();
				i.setName("No items met the search criteria.");
				items = new Item[] { i };
			} else {
				isBlank = false;
				List<Integer> idsList = new ArrayList<Integer>(ids.length);
				for (int id : ids) {
					idsList.add(id);
				}
				items = DataProvider.getDataProvider()
						.requestItemByIDs(idsList);
			}
			itemAdapter = new ItemAdapter(this, items);
			listView.setAdapter(itemAdapter);
		}
		listView.setOnItemClickListener(this);
		edit.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				listView.setAdapter(null);
				String userTag = s.toString();
				List<Item> filtered = new ArrayList<Item>();
				for (Item item : items) {
					Log.e("TagOfItem",
							item.getName() + " "
									+ Arrays.toString(item.getTags()));
					for (String tag : item.getTags()) {
						if (tag.equals(userTag)) {
							filtered.add(item);
							break;
						}
					}
				}

				Item[] newItems = new Item[filtered.size()];

				itemAdapter = new ItemAdapter(ItemListDisplayActivity.this,
						filtered.toArray(newItems));
				listView.setAdapter(itemAdapter);
				itemAdapter.notifyDataSetChanged();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(final Editable s) {
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(
				com.google.code.rgbgoesbetween.R.menu.activity_home, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Log.d("ITEM_LIST_DISPLAY_ACTIVITY", "isBlank: " + isBlank);
		if (!isBlank) {
			Log.d("ITEM_LIST_DISPLAY_ACTIVITY",
					"Id: " + itemAdapter.getItem(position).getId());
			Intent i = new Intent(this, ItemDetailActivity.class);
			i.putExtra(GlobalConstants.ITEM_ID, itemAdapter.getItem(position)
					.getId());
			this.startActivity(i);
		}
	}
}
